/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-08-27 22:34:49
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-10-15 16:51:21
 * @FilePath: \keep_motion_api\utils\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
function escapeRegex(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

function clearObject(obj, ...args) {
  for (const key in obj) {
    if (obj[key] === undefined || obj[key] === null || obj[key] === '') {
      delete obj[key];
    }
  }
  return obj;
}
async function wxLogin(code) {
  // 调用微信登录接口
  try {
    const url = new URL('https://api.weixin.qq.com/sns/jscode2session');
    url.searchParams.append('appid', process.env.APP_ID || 'wxcfd52eb18b628d86');
    url.searchParams.append('secret', process.env.APP_SECRET || 'b8b7809bc03d7cb9031a8e1faf7f9fd1');
    url.searchParams.append('js_code', code);
    url.searchParams.append('grant_type', 'authorization_code');
    const wxLoginResult = await fetch(url, {
      method: 'GET'
    });
    if (!wxLoginResult.ok) {
      throw new Error(`微信登录接口返回错误状态码: ${wxLoginResult.status}`);
    }
    return await wxLoginResult.json();
  } catch (error) {
    throw error;
  }
}

module.exports = {
  escapeRegex,
  clearObject,
  wxLogin
}
